package com.app.micaihu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.app.micaihu.R;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.bean.news.ViewNewsBigImg;
import com.app.micaihu.bean.news.ViewNewsGameOneSmall;
import com.app.micaihu.bean.news.ViewNewsNoImg;
import com.app.micaihu.bean.news.ViewNewsSmallImg;
import com.app.micaihu.bean.news.ViewNewsThreeImg;
import com.app.micaihu.bean.news.ViewPicBigImg;
import com.app.micaihu.bean.news.ViewVideoBigImg;
import com.app.micaihu.bean.news.ViewVideoSmallImg;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.utils.util.LogUtils;

import java.util.List;

/**
 * 搜索列表的适配器
 *
 * @author linkai
 */
public class NewsSearchdapter extends AppBaseAdapter<NewsEntity> {

    private static final int NEWS_NO_IMAGE = 0;// 文章没有图片
    private static final int NEWS_ONE_IMAGE = 1;// 文章一个图片
    private static final int NEWS_THREE_IMAGE = 2;// 文章三个图片
    private static final int NEWS_BIG_IMAGE = 3;// 文章大图(文章  图集 视频)
    private static final int PIC_BIG_IMAGE = 4;// 图片大图（图集  视频）
    private static final int VIDEO_ONE_IMAGE = 5;// 视频一张小图
    private static final int VIDEO_BIG_IMAGE = 6;// 视频一张大图
    private static final int GAME_ONE_IMAGE = 7;//游戏下载一张图

    private String type;//当前适配的栏目类型
    public String matchStr;//匹配的字符串

    public NewsSearchdapter(List<NewsEntity> list, Context context, String type) {
        super(list, context);
        this.type = type;
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        // 获取position对应的数据
        NewsEntity news = list.get(position);
        if (news == null) {
            news = new NewsEntity();
        }
        int type = getItemViewType(position);
        if (type == VIDEO_ONE_IMAGE || type == VIDEO_BIG_IMAGE) {
            MobclickAgentUtils.event(UmEventId.videolist_exposed, UmEventId.INSTANCE.getSearch());
        } else if (type == GAME_ONE_IMAGE) {
            MobclickAgentUtils.event(UmEventId.game_exposed, UmEventId.INSTANCE.getSearchsuccess());
        }
        if (convertView == null) {
            switch (type) {
                case NEWS_NO_IMAGE://没有图
                    convertView = inflater.inflate(R.layout.item_newslist_no_image, null);
                    ViewNewsNoImg viewNewsNoImg = new ViewNewsNoImg(convertView);
                    viewNewsNoImg.loadData(news, matchStr);
                    break;
                case NEWS_ONE_IMAGE://一张图
                    convertView = inflater.inflate(R.layout.item_newslist_normal, null);
                    ViewNewsSmallImg viewNewsSmallImg = new ViewNewsSmallImg(convertView);
                    viewNewsSmallImg.loadData(news, matchStr);
                    break;
                case NEWS_THREE_IMAGE://三张图
                    convertView = inflater.inflate(R.layout.item_newslist_three, null);
                    ViewNewsThreeImg viewNewsThreeImg = new ViewNewsThreeImg(convertView);
                    viewNewsThreeImg.loadData(news, matchStr);
                    break;
                case NEWS_BIG_IMAGE://文章大图(适配文章 图集 视频的大图)
                    convertView = inflater.inflate(R.layout.item_newslist_big_news_image, null);
                    ViewNewsBigImg viewNewsBigImg = new ViewNewsBigImg(convertView);
                    viewNewsBigImg.loadData(news, matchStr);
                    break;
                case PIC_BIG_IMAGE://图集大图（适配图集列表 ）
                    convertView = inflater.inflate(R.layout.item_newslist_big_pic_img, null);
                    ViewPicBigImg viewPicBigImg = new ViewPicBigImg(convertView);
                    viewPicBigImg.loadData(news, matchStr);
                    break;
                case VIDEO_BIG_IMAGE://视频大图（适配图视频列表）
                    convertView = inflater.inflate(R.layout.item_newslist_big_video_img, null);
                    ViewVideoBigImg viewVideoBigImg = new ViewVideoBigImg(convertView);
                    viewVideoBigImg.loadData(news, matchStr);
                    break;
                case VIDEO_ONE_IMAGE://小图视频
                    convertView = inflater.inflate(R.layout.item_newslist_video_normal, null);
                    ViewVideoSmallImg viewVideoSmallImg = new ViewVideoSmallImg(convertView);
                    viewVideoSmallImg.loadData(news, matchStr);
                    break;
                case GAME_ONE_IMAGE://游戏
                    convertView = inflater.inflate(R.layout.item_newslist_game, null);
                    ViewNewsGameOneSmall viewNewsGameOneSmall = new ViewNewsGameOneSmall(convertView);
                    viewNewsGameOneSmall.loadData(news, matchStr, position);
                    break;
                default:
                    convertView = inflater.inflate(R.layout.item_layout_gone, null);
                    break;
            }
        } else {
            switch (type) {
                case NEWS_NO_IMAGE:
                    ViewNewsNoImg viewNewsNoImg = (ViewNewsNoImg) convertView.getTag();
                    viewNewsNoImg.loadData(news, matchStr);
                    break;
                case NEWS_ONE_IMAGE:
                    ViewNewsSmallImg viewNewsSmallImg = (ViewNewsSmallImg) convertView.getTag();
                    viewNewsSmallImg.loadData(news, matchStr);
                    break;
                case NEWS_THREE_IMAGE:
                    ViewNewsThreeImg viewNewsThreeImg = (ViewNewsThreeImg) convertView.getTag();
                    viewNewsThreeImg.loadData(news, matchStr);
                    break;
                case NEWS_BIG_IMAGE:
                    ViewNewsBigImg viewNewsBigImg = (ViewNewsBigImg) convertView.getTag();
                    viewNewsBigImg.loadData(news, matchStr);
                    break;
                case PIC_BIG_IMAGE:
                    ViewPicBigImg viewPicBigImg = (ViewPicBigImg) convertView.getTag();
                    viewPicBigImg.loadData(news, matchStr);
                    break;
                case VIDEO_BIG_IMAGE:
                    ViewVideoBigImg viewVideoBigImg = (ViewVideoBigImg) convertView.getTag();
                    viewVideoBigImg.loadData(news, matchStr);
                    break;
                case VIDEO_ONE_IMAGE:
                    ViewVideoSmallImg viewVideoSmallImg = (ViewVideoSmallImg) convertView.getTag();
                    viewVideoSmallImg.loadData(news, matchStr);
                    break;
                case GAME_ONE_IMAGE://游戏
                    ViewNewsGameOneSmall viewNewsGameOneSmall = (ViewNewsGameOneSmall) convertView.getTag();
                    viewNewsGameOneSmall.loadData(news, matchStr, position);
                    break;
                default:
                    convertView = inflater.inflate(R.layout.item_layout_gone, null);
                    break;
            }
        }

        return convertView;
    }


    @Override
    public int getViewTypeCount() {
        return 9;
    }

    /**
     * 得到UI类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        NewsEntity news = list.get(position);
        if (news == null) {
            return NEWS_NO_IMAGE;
        }
        // 文章类型(1.普通文章、2.普通图集、3普通视频 4.电商文章 5.网页文章---只显示1,2,3,10
        // 6.电商网页软文 7.网页视频 8.轻专题 9.广告)
        switch (news.getArticleType()) {
            //适配所有文章
            case TypeConstant.NEWS_NORMAL:
            case TypeConstant.NEWS_SHOP:
            case TypeConstant.NEWS_WEB:
            case TypeConstant.NEWS_SHOP_WEB:
            case TypeConstant.NEWS_TOPIC:
            case TypeConstant.GAME_RAIDERS_TEXT://游戏图文攻略
            case TypeConstant.GAME_RAIDERS_VIDEO://游戏视频攻略
                //适配话题
//            case TypeConstant.TOPIC_POST:
            {
                if (news.getArticleThumb() == null || news.getArticleThumb().length == 0) {
                    return NEWS_NO_IMAGE;
                } else if (TypeConstant.NEWS_UI_SMALL_IMG.equals(news.getUiType())) {
                    //小图
                    if (news.getArticleThumb().length >= 1 && news.getArticleThumb().length < 3) {
                        return NEWS_ONE_IMAGE;
                    } else if (news.getArticleThumb().length >= 3) {
                        return NEWS_THREE_IMAGE;
                    } else {
                        return NEWS_NO_IMAGE;
                    }
                } else {
                    //大图
                    return NEWS_BIG_IMAGE;
                }
            }
            //适配所有图集
            case TypeConstant.PIC_NORMAL:
            case TypeConstant.PIC_TOPIC: {
                if (news.getArticleThumb() == null || news.getArticleThumb().length == 0) {
                    return NEWS_NO_IMAGE;
                } else if (TypeConstant.NEWS_UI_SMALL_IMG.equals(news.getUiType())) {
                    return NEWS_ONE_IMAGE;
                } else if (TypeConstant.CHANNEL_TYPE_PICS.equals(this.type)
                        && TypeConstant.NEWS_UI_BIG_TWO_IMG.equals(news.getUiType())) {
                    return PIC_BIG_IMAGE;
                } else {
                    return NEWS_BIG_IMAGE;
                }
            }
            //适配所有视频
            case TypeConstant.VIDEO_ORIGINAL:
            case TypeConstant.VIDEO_NORMAL:
            case TypeConstant.VIDEO_WEB: {
                if (news.getArticleThumb() == null || news.getArticleThumb().length == 0) {
                    return NEWS_NO_IMAGE;
                } else if (TypeConstant.NEWS_UI_SMALL_IMG.equals(news.getUiType())) {
                    return VIDEO_ONE_IMAGE;
                } else if (TypeConstant.NEWS_UI_BIG_TWO_IMG.equals(news.getUiType())
                        && TypeConstant.CHANNEL_TYPE_VIDEO.equals(this.type)) {
                    return VIDEO_ONE_IMAGE;
                } else {
                    return VIDEO_ONE_IMAGE;
                }
            }
            //适配游戏
            case TypeConstant.GAME_VIDEO:
                return GAME_ONE_IMAGE;
            default:
                return NEWS_NO_IMAGE;
        }
    }

}
